package org.csu.springjpetstore.service;


import org.csu.springjpetstore.domain.LineItem;
import org.csu.springjpetstore.domain.Order;
import org.csu.springjpetstore.domain.Sequence;
import org.csu.springjpetstore.persistence.ItemDAO;
import org.csu.springjpetstore.persistence.LineItemDAO;
import org.csu.springjpetstore.persistence.OrderDAO;
import org.csu.springjpetstore.persistence.SequenceDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {

    @Autowired(required = false)
    private OrderDAO orderDAO;

    @Autowired
    private ItemDAO itemDAO;
    @Autowired
    private SequenceDAO sequenceDAO;
    @Autowired
    private LineItemDAO lineItemDAO;
//    private final ItemDAO itemDAO = new ItemDAOImpl();
//
//    private final SequenceDAO sequenceDAO = new SequenceDAOImpl();
//    private final LineItemDAO lineItemDAO = new LineItemDAOImpl();

    public List<Order> getConfirmByusername(String username) {
        return orderDAO.getOrdersByUsername(username);
    }

    public Order getConfirmByOrderID(int orderId) {

        return orderDAO.getOrderByOrderId(orderId);
    }

    //    public void InsertOrder(Order order){
//
//        orderDAO.insertOrder(order);
//    }
//
//    public void  InsertOrderStatus(Order order)
//    {
//
//        orderDAO.insertOrderStatus(order);
//    }
    public void insertOrders(Order order) {


        order.setOrderId(getNextId("ordernum"));

        //更新存货信息
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            String itemId = lineItem.getItemId();
            Integer itemQuantity = lineItem.getQuantity();
            Map<String, Object> param = new HashMap<String, Object>(2);
            param.put("itemId", itemId);
            param.put("itemQuantity", itemQuantity);
            //todo 调试
            System.out.println(itemQuantity);
            itemDAO.updateInventoryQuantity(param);
        }

        //更新数据库orders 和 orderstatus 的信息
        orderDAO.insertOrder(order);
        orderDAO.insertOrderStatus(order.getOrderId(), order.getLineItems().size(), new Date(order.getOrderDate().getTime()), order.getStatus());
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemDAO.insertLineItem(lineItem);
        }
    }


    public int getNextId(String name) {
        Sequence sequence;
        sequence = sequenceDAO.getSequenceByName(name);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name + " sequence).");
        }
        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
        sequenceDAO.updateSequence(parameterObject);
        return sequence.getNextId();
    }

}
